﻿# -*- coding: utf-8 -*

from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select
from django.utils.translation import ugettext_lazy as _

from utils import test_inn, test_inn_org, test_inn_person

class RUPostalCodeField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid postal code in format XXXXXX.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUPostalCodeField, self).__init__(r'^\d{6}$', *args, **kwargs)

class RUKPPNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid KPP number in format XXXXXXXXX.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUKPPNumberField, self).__init__(r'^\d{9}$', *args, **kwargs)

class RUINNNumberField(Field):
    """
    A form field that validates Russian INN numbers.
    """
    default_error_messages = {
        'invalid': _(u'Enter a valid INN number.'),
    }

    def clean(self, value):
        value = super(RUINNNumberField, self).clean(value)
        if value == u'':
            return value
        try:
            inn_number = int(value)
        except ValueError:
            raise ValidationError(self.error_messages['invalid'])

        if not self.has_valid(value):
            raise ValalidationError(self.error_messages['invalid'])

        return smart_unicode(value)

    def has_valid(self, value):
        return test_inn(value)

class RUINNOrgNumberField(RUINNNumberField):
    """
    A form field that validates Russian INN numbers for orgs.
    """
    def has_valid(self, value):
        return test_inn_org(value)

class RUINNPersonNumberField(RUINNNumberField):
    """
    A form field that validates Russian INN numbers for persons.
    """
    def has_valid(self, value):
        return test_inn_person(value)

class RUBICNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid bank account number ' 
                     'in format XXXXXXXXX.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUBICNumberField, self).__init__(
            r'^\d{9}$', *args, **kwargs)


class RUBankAccountNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid bank account number ' 
                     'in format XXXXXXXXXXXXXXXXX.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUBankAccountNumberField, self).__init__(
            r'^\d{20}$', *args, **kwargs)

class RUBankCorrAccountNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid bank correspondent account number ' 
                     'in format 301XXXXXXXXXXXXXX.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUBankCorrAccountNumberField, self).__init__(
            r'^301\d{17}$', *args, **kwargs)

class RUPFRFCertificateNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid PFR certificate number in format XXX-XXX-XXX-XX or XXX-XXX-XXX XX.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUPFRFCertificateNumberField, self).__init__(
            r'^\d{3}-\d{3}-\d{3}(?:-| )\d{2}$', *args, **kwargs)

class RUCarNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid car number in format AXXXAA/XXX, AXXXAA/XX '
                     'or AXXXAA XXX, AXXXAA XX.'),
    }
    def __init__(self, *args, **kwargs):
        eng_letters = u'ABCEHKMOPTYX'
        rus_letters = u'АВСЕНКМОРТУХ'
        letters_together = u'%s%s%s%s' % (eng_letters, eng_letters.lower(),
                                          rus_letters, rus_letters.lower())
        super(RUCarNumberField, self).__init__(
            ru'^[%s]\d{3}[%s]{2}(?: |/)\d{2,3}$' % (letters_together,) * 2,
            *args, **kwargs)


class RUStateSelect(Select):
    """
    A Select widget that uses a list of Russian states/territories as its choices.
    """
    def __init__(self, attrs=None):
        from ru_states import RU_STATES
        super(RUStateSelect, self).__init__(attrs, choices=RU_STATES)

class RUOGRNNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid OGRN number in format 13 digits.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUOGRNNumberField, self).__init__(r'^\d{13}$', *args, **kwargs)

class RUOGRNIPNumberField(RegexField):
    default_error_messages = {
        'invalid': _('Enter a valid OGRNIP number in format 15 digits.'),
    }
    def __init__(self, *args, **kwargs):
        super(RUOGRNNumberField, self).__init__(r'^\d{15}$', *args, **kwargs)
